package cn.ipanel.bigdata.boot.logger

import cn.ipanel.bigdata.boot.Application
import cn.ipanel.bigdata.utils.Util._

/**
 * Author: lzz
 * Date: 2021/11/16 16:15
 */
abstract class Logger(name: String, level: Int) extends Console {

  private[this] val INFO:  String = "INFO "
  private[this] val DEBUG: String = "DEBUG"
  private[this] val ERROR: String = "ERROR"
  private[this] val WARN:  String = "WARN "
  private[this] val LEVEL: Int = level

  def E(args: Any): Unit = error(args)
  def error(args: Any): Unit = if (LEVEL >= Logger.LEVEL_ERROR) _log(ERROR, args.toString)
  def W(args: Any): Unit = warn(args)
  def warn (args: Any): Unit = if (LEVEL >= Logger.LEVEL_WARN)  _log(WARN,  args.toString)
  def I(args: Any): Unit = info(args)
  def info (args: Any): Unit = if (LEVEL >= Logger.LEVEL_INFO)  _log(INFO,  args.toString)
  def D(args: Any): Unit = debug(args)
  def debug(args: Any): Unit = if (LEVEL >= Logger.LEVEL_DEBUG) _log(DEBUG, args.toString)

  override def getName: String = name
}

object Logger {

  val LEVEL_DEBUG: Int = 8
  val LEVEL_INFO:  Int = 4
  val LEVEL_WARN:  Int = 2
  val LEVEL_ERROR: Int = 1
  private[this] val LEVEL: Int = LEVEL_DEBUG
  private[this] val DEFAULT: String = "Main"
  private[this] val _logger: Logger = Logger(DEFAULT)

  def apply(clz: Class[_]) : Logger = new LogImpl(clz.getSimpleName)
  def apply(name: String)  : Logger = new LogImpl(name)

  private[this] class LogImpl(name: String) extends Logger(name, LEVEL) {
    override def getName: String = if (isEmpty(Application.getExecJobName)) DEFAULT else Application.getExecJobName
  }

  def E(args: Any): Unit = _logger.E(args)
  def W(args: Any): Unit = _logger.W(args)
  def I(args: Any): Unit = _logger.I(args)
  def D(args: Any): Unit = _logger.D(args)
}